package com.example.app4.service;

import android.app.IntentService;
import android.app.Notification;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

import com.example.app4.MainActivity;
import com.example.app4.R;
import com.example.app4.adapter.MyAdapter;
import com.example.app4.entil.User;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class TestService3 extends IntentService {
    private final String TAG = "hehe";

    public int count;

    static List<User> listxiaog = new ArrayList<>();
    static List<User> listxiaojiao = new ArrayList<>();
    static List<User> listxiaomao = new ArrayList<>();
    private MyAdapter myAdapter;
    private User user;

    //必须实现父类的构造方法  
    public TestService3()  
    {  
        super("TestService3");  
    }


    //定义onBinder方法所返回的对象
     MyBinder binder = new MyBinder();
    public class MyBinder extends Binder
    {
        public List getCountxiaog()
        {
            //返回这个集合
            return listxiaog;
        }


        public List getCountxiangjiao()
        {
            //返回这个集合
            return listxiaojiao;
        }


        public List getCountxiaomao()
        {
            //返回这个集合
            return listxiaomao;
        }
    }


    //必须重写的核心方法  
    @Override  
    protected void onHandleIntent(Intent intent) {
        //Intent是从Activity发过来的，携带识别参数，根据参数不同执行不同的任务  
        String action = intent.getExtras().getString("param");
        int shuju = intent.getExtras().getInt("shuju");


        if (action.equals("s1")) {
            String name = "xiao gou";

            if (listxiaog != null && listxiaog.size() > 0) {

                listxiaog.clear();
            }

            String[] lines = new String[100000];

            long currentTimeMillis = System.currentTimeMillis();
            for (int i = 0; i <= shuju; i++) {
                user = new User();

                Log.d("ccc", "onHandleIntent: "+user);
                lines[i] = name + String.valueOf(i);
                count = i;

            }
            long endcurrentTimeMillis = System.currentTimeMillis();

           long l= endcurrentTimeMillis- currentTimeMillis;

            Log.d("xxf", "创建时间"+l);

            user.setName(name);
            user.setCount(String.valueOf(count));
            listxiaog.add(user);
            try {

                FileOutputStream fos = openFileOutput("filename1.txt", Context.MODE_PRIVATE);
                for (String line : lines) {
                    String content = line + "\n";  // 加上换行符
                    //写入数据
                    fos.write(content.getBytes());
                }
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

            Log.i(TAG, "启动service1");
        }

          
        //让服务休眠2秒
        try{
            Thread.sleep(2000);
            Log.i(TAG, "执行完了");
        }catch(InterruptedException e){e.printStackTrace();}
    }  
  
    //重写其他方法,用于查看方法的调用顺序  
    @Override  
    public IBinder onBind(Intent intent) {
        Log.i(TAG,"onBind");  
        return binder;
    }  
  
    @Override  
    public void onCreate() {  
        Log.i(TAG,"onCreate");  
        super.onCreate();

      /*  Notification.Builder localBuilder = new Notification.Builder(this);
        localBuilder.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0));
        localBuilder.setAutoCancel(false);
        localBuilder.setSmallIcon(R.drawable.ic_launcher_foreground);
        localBuilder.setTicker("Foreground Service Start");
        localBuilder.setContentTitle("Socket服务端");
        localBuilder.setContentText("正在运行...");
        startForeground(1, localBuilder.getNotification());*/
    }  
  
    @Override  
    public int onStartCommand(Intent intent, int flags, int startId) {  
        Log.i(TAG,"onStartCommand");  
        return super.onStartCommand(intent, flags, startId);  
    }  
  
  
    @Override  
    public void setIntentRedelivery(boolean enabled) {  
        super.setIntentRedelivery(enabled);  
        Log.i(TAG,"setIntentRedelivery");  
    }  
      
    @Override  
    public void onDestroy() {  
        Log.i(TAG,"onDestroy");  
        super.onDestroy();  
    }  
      
} 
